package com.ambow.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.ambow.Entity.Operator;
import com.ambow.dao.Operat;

import java.util.List;



/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
//		String status=request.getParameter("status");
		PrintWriter out = response.getWriter();

		String val=request.getParameter("val");
		String account=request.getParameter("textfield");
		String password=request.getParameter("textfield2");
		System.out.println(account);
		System.out.println(password);
		
		try{
			
		
		Operat person_info = new Operat();
		
		List list=(List) person_info.loginSelect1(account, password);
		Operator op = (Operator) list.get(0);
		if(account.equals(op.getAccount())&&password.equals(op.getPassword())){
			//把操作者信息存入session，并跳转首页
		
			HttpSession session=request.getSession();
			session.setAttribute("operator_id", op.getId());
			session.setAttribute("operator_name", op.getName());
			session.setAttribute("operator_posetion", op.getPosition());
			//String op_postion;
			//request.setAttribute("opPostion", op.getPosetion());
			session.setAttribute("operator_tel", op.getTel());
			//request.setAttribute("opTel", op.getTel());
			session.setAttribute("operator_role", op.getRole());
			//request.setAttribute("opRole", op.getRole());
			session.setAttribute("operator_remark", op.getRemark());
			//request.setAttribute("opRemark", op.getRemark());
			session.setAttribute("operator_password", op.getPassword());
			session.setAttribute("op", op);
			response.sendRedirect("index.jsp");
		}

		else{
			out.print("登录失败");
		}
		
        }catch(Exception e){
			out.print("登录失败！");
			response.sendRedirect("login.jsp");
		    }
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
		
	}

}
